package cn.changshengjue.dakong.api;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.inventory.meta.ItemMeta;

import cn.changshengjue.dakong.main;
import cn.changshengjue.dakong.bean.setting;
import cn.changshengjue.dakong.config.cfg;
import cn.changshengjue.dakong.utils.ItemSerialize;
import cn.changshengjue.dakong.utils.Utils;
import cn.changshengjue.dakong.utils.YmlUtil;

public class apiImpl implements api {

	public Map<String, setting> getSetting() {
		YmlUtil ymlUtil = new YmlUtil();
		File file = ymlUtil.getFile("setting.yml");
		FileConfiguration fileMine = ymlUtil.getFileMine(file);
		Set<String> keys = fileMine.getKeys(false);

		Map<String, setting> map = new HashMap<String, setting>();
		for (String s : keys) {
			setting setting = new setting();
			setting.setK_name(fileMine.getString(s + ".name").replace("&", "§"));
			setting.setK(s);
			setting.setK_lore(fileMine.getString(s + ".lore").replace("&", "§"));
			setting.setK_replaceLore(fileMine.getString(s + ".replaceLore").replace("&", "§"));
			setting.setK_random_lucky(fileMine.getString(s + ".random.lucky"));
			setting.setK_random_probability(fileMine.getString(s + ".random.probability"));
			setting.setK_random_number(fileMine.getString(s + ".random.number"));
			setting.setK_random_damageProbability(fileMine.getString(s + ".random.damageProbability"));
			setting.setK_random_first(fileMine.getBoolean(s + ".random.first"));
			setting.setK_random_noDamage(fileMine.getString(s + ".random.noDamage"));
			setting.setK_random_noDamageNum(fileMine.getInt(s + ".random.noDamageNum"));
			setting.setK_level_attributeLevel(fileMine.getString(s + ".level.attributeLevel"));
			setting.setK_type(fileMine.getStringList(s + ".type"));
			setting.setK_consume_vitality(fileMine.getString(s + ".consume.vitality"));
			setting.setK_consume_consumeItem(fileMine.getBoolean(s + ".consume.consumeItem"));
			map.put(s, setting);
		}
		return map;
	}

	/**
	 * 获取成功几率 计算后
	 * 
	 * @param probability
	 * @param player
	 * @return
	 */
	public Double getProbability(String probability, Player player) {
		cn.changshengjue.lucky.api.LuckyApi api = new cn.changshengjue.lucky.api.LuckyApi();
		Map<String, Object> luckyAndVitality = api.getLuckyAndVitality(player.getName());

		// 判断是否有幸运
		if (luckyAndVitality.size() != 0) {
			Double lucky = Double.valueOf(
					Utils.jisuan(probability.replace("%lucky%", luckyAndVitality.get("lucky").toString())).toString());
			return lucky;
		}

		return 0.00;

	}

	/**
	 * 获取不损坏几率 计算后
	 * 
	 * @param probability
	 * @param player
	 * @return
	 */
	public Double getDamageProbability(String damageProbability, Player player) {
		cn.changshengjue.lucky.api.LuckyApi api = new cn.changshengjue.lucky.api.LuckyApi();
		Map<String, Object> luckyAndVitality = api.getLuckyAndVitality(player.getName());

		// 判断是否有幸运
		if (luckyAndVitality.size() != 0) {
			Double lucky = Double.valueOf(Utils
					.jisuan(damageProbability.replace("%lucky%", luckyAndVitality.get("lucky").toString())).toString());
			return lucky;
		}

		return 0.00;

	}

	/**
	 * 获取 lore中有几个已开孔
	 * 
	 * @param replaceLore
	 * @param lore
	 * @return
	 */
	public int getYiKaiKong(String replaceLore, List<String> lore) {
		int a = 1;
		for (String s : lore) {
			if (s.equals(replaceLore)) {
				a++;
			}
		}
		return a;
	}

	/**
	 * 获取 装备等级
	 * 
	 * @param replaceLore
	 * @param lore
	 * @return
	 */
	public int getItemLevel(ItemStack item) {
		String AttributePluginsLevel = main.getInstance().getConfig().getString("AttributePluginsLevel");
		for (String s : item.getItemMeta().getLore()) {
			if (s.contains(AttributePluginsLevel)) {
				return Integer.valueOf(Utils.getNumber(Utils.strColorRemove(s)));
			}
		}
		return -1;
	}

	/**
	 * 获取 幸运公式结果
	 * 
	 * @return
	 */
	public String getLuckyResult(String lucky) {
		if (!lucky.contains("-")) {
			if (Utils.isNumber(lucky)) {
				return lucky;
			} else {
				return 1 + "";
			}
		} else {
			String[] s = lucky.split("-");
			double randomNumberInRange = Utils.getRandomNumberInRange(Double.valueOf(s[0]), Double.valueOf(s[1]));

			long round = Math.round(randomNumberInRange);
			return randomNumberInRange + "";
		}
	}

	/**
	 * 玩家是否开启防爆
	 * 
	 * @param name
	 * @return
	 */
	public boolean isFB(String name) {
		YmlUtil ymlUtil = new YmlUtil();
		File file = ymlUtil.getFile("playerDate.yml");
		FileConfiguration fileMine = ymlUtil.getFileMine(file);
		return fileMine.getBoolean(name);

	}

	/**
	 * 玩家背包是否有足够防爆
	 * 
	 * @param name
	 * @return
	 */
	public boolean isFBNumber(String fbyml, Player p) {
		YmlUtil ymlUtil = new YmlUtil();
		File file = ymlUtil.getFile("item.yml");
		FileConfiguration fileMine = ymlUtil.getFileMine(file);
		String string = fileMine.getString(fbyml);
		if (string != null) {
			ItemStack item = new ItemSerialize().toItem(string);
			for (ItemStack i : p.getInventory()) {
				if (Utils.itemDisplayNameIsNull(i) == false && Utils.itemDisplayNameIsNull(i) == false) {
					if (i.getItemMeta().getDisplayName().equals(item.getItemMeta().getDisplayName())) {
						return true;
					}
				}
			}
		}
		return false;
	}

	/**
	 * 获取防爆宝石
	 * 
	 * @param name
	 * @return
	 */
	public ItemStack getFB(String fbyml) {
		YmlUtil ymlUtil = new YmlUtil();
		File file = ymlUtil.getFile("item.yml");
		FileConfiguration fileMine = ymlUtil.getFileMine(file);
		String string = fileMine.getString(fbyml);
		ItemStack item = new ItemSerialize().toItem(string);

		return item;
	}

}
